//
//  LoginViewController.m
//  MVP
//
//  Created by 曹敬贺 on 16/8/4.
//  Copyright © 2016年 北京无限点乐科技有限公司. All rights reserved.
//

#import "LoginViewController.h"
#import "MainViewController.h"


@interface LoginViewController ()<LoginPresenterDelegate>

@property (nonatomic, strong) UITextField * nameTF;
@property (nonatomic, strong) UITextField * passwordTF;
@property (nonatomic, strong) UIButton * loginButton;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.nameTF];
    [self.view addSubview:self.passwordTF];
    [self.view addSubview:self.loginButton];
    [self updateViewConstraints];
}
- (void)updateViewConstraints
{
    [super updateViewConstraints];
//    CGFloat center = CGRectGetMidX(self.view.bounds);
    self.nameTF.frame = CGRectMake(50, 100, 200, 40);
    self.passwordTF.frame = CGRectMake(50, 200, 200, 40);
    self.loginButton.frame = CGRectMake(50, 300, 200, 40);
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.nameTF resignFirstResponder];
    [self.passwordTF resignFirstResponder];
}
#pragma mark - LazyUI
- (LoginPresenter *)presenter
{
    if (! _presenter)
    {
        _presenter = [[LoginPresenter alloc]init];
        _presenter.delegate = self;
    }
    return _presenter;
}

- (UITextField *)nameTF
{
    if (! _nameTF)
    {
        _nameTF = [[UITextField alloc]init];
        _nameTF.placeholder = @"name";
        _nameTF.backgroundColor = [UIColor blueColor];
    }
    return _nameTF;
}
- (UITextField *)passwordTF
{
    if (! _passwordTF)
    {
        _passwordTF = [[UITextField alloc]init];
        _passwordTF.placeholder = @"password";
        _passwordTF.backgroundColor = [UIColor blueColor];
    }
    return _passwordTF;
}
- (UIButton *)loginButton
{
    if (! _loginButton)
    {
        _loginButton = [[UIButton alloc]init];
        [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
        _loginButton.backgroundColor = [UIColor greenColor];
        extern SEL loginClick;
        [_loginButton addTarget:self.presenter action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginButton;
}

#pragma mark - PresenterDelegate

- (NSString *)userName
{
    return self.nameTF.text;
}
- (NSString *)userPassword
{
    return self.passwordTF.text;
}
- (void)updateUI:(LoginType)type With:(User *)user
{
    switch (type) {
        case LoginSuccess:
        {
            NSLog(@"登录成功");
            MainViewController * mainVC = [[MainViewController alloc] init];
            mainVC.user = user;
            [self.navigationController pushViewController:mainVC animated:YES];
        }
            break;
        case LoginFailed:
            NSLog(@"登录失败");
            break;
        default:
            break;
    }
}

@end
